﻿layui.use(['table', 'laypage'], function () {
    var table = layui.table;
    var laypage = layui.laypage;
    var $ = layui.$;
    var layer = layui.layer; // 添加layui弹出层模块

    var pageSize = 15; // 每页显示条数
    //获取列表
    function getListData(pageIndex) {
        //获取查询条件
        var EmployeeName = $('#EmployeeName').val();
        var functionName = $('#functionName').val();
        var DepartId = $('#DepartId').val();
        var EmployeeState = $('#EmployeeState').val();
        //ajax后台交互
        $.ajax({
            url: 'http://10.31.60.18:8000/AuthorizationPeopleGetRout/api/RoleShow/ShowPageEmployeeInfo',
            type: 'get',
            dataType: 'json',
            //参数
            data: {
                pageIndex: pageIndex,
                pageSize: pageSize,
                EmployeeName: EmployeeName,
                DepartId: DepartId,
                EmployeeState: EmployeeState,
                //DepartId: DepartId,
            },
            success: function (res) {
                table.render({
                    //挂载的表格id
                    elem: '#ID-table-demo-setRowChecked',
                    cols: [[
                        { type: 'checkbox', width: 60, fixed: 'center', unresize: true, align: 'center' },
                        { type: 'numbers', title: '员工编号', width: 60, fixed: 'center', unresize: true, align: 'center' },
                        { field: 'employeeName', title: '姓名', width: 120, align: 'center' },
                        { field: 'departId', title: '部门', width: 120, align: 'center' },
                        { field: 'employeePhone', title: '联系方式', width: 120, align: 'center' },
                        { field: 'employeeState', title: '员工状态', width: 120, align: 'center' },
                        { field: 'createBy', title: '操作人', width: 120, align: 'center' },
                        { field: 'createDate', title: '操作时间', width: 120, align: 'center', templet: function (data) { return data.createDate.substring(0, 10) } },
                        { field: 'updateBy', title: '修改人', width: 120, align: 'center' },
                        { field: 'updateDate', title: '修改时间', width: 120, align: 'center', templet: function (data) { return data.updateDate.substring(0, 10) } },
                        { title: '操作', width: 120, align: 'center', templet: '#barDemo', fixed: 'right', unresize: true },
                    ]],
                    data: res.data
                });

                //分页
                laypage.render({
                    //挂载的分页id
                    elem: 'pagesbox',
                    count: res.totalCount,
                    limit: pageSize,
                    curr: pageIndex,
                    layout: ['prev', 'page', 'count', 'next', 'skip'],
                    jump: function (obj, first) {
                        if (!first) {
                            getListData(obj.curr);
                        }
                    }
                });
            }
        });
    }
    //点击事件触发显示功能   查询
    $('#searchBtn').on('click', function () {
        getListData(1);
    });

    // 单删除
    function deleteData(roleId) {
        $.ajax({
            url: `http://10.31.60.18:8000/AuthorizationPeopleSetRout/api/Employee/FalseDelete?roleId=${roleId}`,
            type: 'delete',
            success: function (res) {
                if (res > 0) {
                    layer.msg('删除成功');
                    getListData(1);
                } else {
                    layer.msg('删除失败');
                    getListData(1);
                }
            },
            error: function () {
                layer.msg('删除请求失败');
            }
        });
    }

    // 监听工具条，实现删除功能
    table.on('tool(ID-table-demo-setRowChecked)', function (obj) {
        var data = obj.data;
        if (obj.event == 'delete') {
            layer.confirm('确定删除吗？', function (index) {
                deleteData(data.roleId); // 传递正确的数据项ID
                layer.close(index);
            });
        }
    });

    // 批量删除
    $(".delAll_btn").click(function () {
        var checkStatus = table.checkStatus('ID-table-demo-setRowChecked'),
            data = checkStatus.data,
            roleId = "";
        if (data.length > 0) {
            for (var i in data) {
                roleId += data[i].roleId + ",";
            }
            roleId = roleId.substring(0, roleId.length - 1);
            console.log(roleId);
            layer.confirm('确定删除选中的记录？', { icon: 3, title: '提示信息' }, function (index) {
                $.ajax({
                    url: `http://10.31.60.18:8000/AuthorizationPeopleSetRout/api/Employee/AllFalseDeleteRole?roleId=${roleId}`,
                    type: 'put',
                    success: function (res) {
                        if (res > 0) {
                            layer.msg("批量删除成功");
                            getListData(1);
                        } else {
                            layer.msg("批量删除失败");
                        }
                    },
                    error: function () {
                        layer.msg('批量删除请求失败');
                    }
                });
                layer.close(index);
            });
        } else {
            layer.msg("请选择需要删除的记录");
        }
        console.log(checkStatus);
    });

    // 监听工具条，实现批量删除功能
    table.on('tool(ID-table-demo-setRowChecked)', function (obj) {
        var data = obj.data;
        if (obj.event == 'delete') {
            layer.confirm('确定删除吗？', function (index) {
                deleteData(data.roleId);
                layer.close(index);
            });
        }
    });

    //首次加载调用
    $(function () {
        // 初始化加载数据
        getListData(1);
    })
});